<?php
class Usuario_model extends CI_Model
{
	public function  __construct()
	{
		$this->load->database();		
	} 	
	
	public function verifica_username($username) {
        
		$this->db->where('NickName',$username);
        $query = $this->db->get('usuario');
        
        return  $query->num_rows();
    }
 
    public function verifica_email($email) {
        
    	$this->db->where('Email',$email);
        $query = $this->db->get('usuario');
        
        return $query->num_rows();
    }
	
	public function registar($usuario)
	{
		return $this->db->insert('usuario',$usuario);
	}
	
	public function update_avatar($id,$avatar)
	{
		$data = array('avatar'=>$avatar);
		
		$this->db->where('id',$id);
		return $this->db->update('usuario',$data);
	}
	
 	public function login_user($username,$password)
    {
    	date_default_timezone_set('America/Bogota');
    	
        $this->db->where('NickName',$username);
        $this->db->where('pws',$password);
        
        $query = $this->db->get('usuario');
        
        if($query->num_rows() == 1)
        {
        	$row = $query->row();        	
        	
        	$data = array('ultimoLogin'=>date("Y-m-d H:i:s"));		
			$this->db->where('id',$row->id);
			$this->db->update('usuario',$data);       	
        	
            return $row;
        }else{
            $this->session->set_flashdata('usuario_incorrecto','Los datos introducidos son incorrectos'.$username.$password);
            redirect(base_url().'login.html','refresh');
        }
    }
}
?>